//
//  PollingProgressCell.m
//  OperationPro
//
//  Created by yaxue wang on 2017/1/5.
//  Copyright © 2017年 展鸿科技. All rights reserved.
//

#import "PollingProgressCell.h"
#import "PollingProgressModel.h"
@interface PollingProgressCell()



@end
@implementation PollingProgressCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _topLine = [[UIImageView alloc] init];
        [self addSubview:_topLine];
        
        _belowLine = [[UIImageView alloc] init];
        [self addSubview:_belowLine];
        
        
        //进度点img
        _headImg=[[UIImageView alloc]initWithFrame:CGRectMake(35, 15, 10, 10)];
        [self addSubview:_headImg];
        
        //状态
        _stateLable=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_headImg.frame)+14, 10, 100, 17)];
        [self addSubview:_stateLable];
        
        _stateLable.font=F(14);
        _stateLable.textColor=FontColor;
        //接单显示
        UIImage*phoneImage=[UIImage imageNamed:@"Ico_home_detailPhone"];
        _phoneView=[[UIView alloc]initWithFrame:CGRectMake(60, 36, WindowWidth-60, phoneImage.size.height)];
        [self addSubview:_phoneView];
        
        _nameLab= [[UILabel alloc ]init];
        _nameLab.userInteractionEnabled=YES;
        [self.phoneView addSubview:_nameLab];
        [_nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.phoneView).offset(0);
            make.top.equalTo(self.phoneView).offset(0);
            
        }];
        _nameLab.font=F(14);
        _nameLab.userInteractionEnabled =YES;
        _nameLab.textColor=MainColor;
        //电话按钮
        _phoneImg=[[UIImageView alloc]init];
        [self.phoneView addSubview:_phoneImg];
        _phoneImg.userInteractionEnabled=YES;
        [_phoneImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_nameLab);
            make.left.equalTo(_nameLab.mas_right).offset(12);
            make.size.mas_equalTo(CGSizeMake(phoneImage.size.width, phoneImage.size.height));
        }];
        //内容详情
        _contentLab= [[UILabel alloc ]init];
        [self addSubview:_contentLab];
        [_contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_nameLab);
            make.right.equalTo(self).offset(-15);
            make.bottom.equalTo(self.mas_bottom).offset(-15);
            
        }];
        _contentLab.userInteractionEnabled=YES;
        _contentLab.font=F(12);
        _contentLab.textColor=LightFontColor;
        _contentLab.numberOfLines=0;
        
        //时间
        _timeLab=[[UILabel alloc]initWithFrame:CGRectMake(WindowWidth-160, 15,150,14)];
        _timeLab.font=F(12);
        _timeLab.textAlignment =NSTextAlignmentRight;
        _timeLab.textColor=LightFontColor;
        [self addSubview:_timeLab];
        
        //横线
        CGFloat width=10+35+14;
        
        _lineImg=[[UIImageView alloc]init];
        [self addSubview:_lineImg];
        UIImage *img=[UIImage imageNamed:@"Ico_common_line"];
        [_lineImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(width);
            make.right.equalTo(self).offset(0);
            make.size.mas_equalTo(CGSizeMake(img.size.width, img.size.height));
            make.bottom.equalTo(self.mas_bottom).offset(-img.size.height);
            
        }];
        
        _lineImg.image=img;
        
    }
    return self;
}
- (void)setModel:(id)model{
    PollingProgressModel *order = model;
    if (order.P_state_name==nil||[order.P_state_name isEqualToString:@""]) {
        self.stateLable.text =@"";
        
    }else
    {
        _stateLable.text =order.P_state_name;
        
    }
    NSInteger num=[order.P_state integerValue];
    
    //确认
    if(num==90)//确认状态界面特殊处理
    {
        UIImage *img=[UIImage imageNamed:@"Ico_progree_green"];
        self.headImg.image=img;
        [_headImg mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(35-(25-10)/2);
            make.size.mas_equalTo(CGSizeMake(25, 25));
            make.top.equalTo(self).offset(10);
        }];
        [_stateLable mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(59);
            make.right.equalTo(self).offset(-15);
            make.centerY.equalTo(self.headImg);
            
        }];
        _stateLable.textColor=MainColor;
        
    }else
    {
        _stateLable.textColor=FontColor;
        UIImage *img=[UIImage imageNamed:@"Ico_progree_green"];
        self.headImg.image=img;
        [_headImg mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(35);
            make.size.mas_equalTo(CGSizeMake(10, 10));
            make.top.equalTo(self).offset(15);
        }];
        [_stateLable mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.headImg.mas_right).offset(14);
            make.right.equalTo(self).offset(-15);
            make.centerY.equalTo(self.headImg);
            
        }];
    }
    
    
    if(num==90)//确认
    {
        CGSize labelsize = CGSizeZero;
        CGFloat width=10+35+14;
        
        if ([order.P_content isKindOfClass:[NSNull class]]||[order.P_content isEqualToString:@""]) {
            
        }else
        {
            
            labelsize = [order.P_content sizeWithFont:F(12) constrainedToSize:CGSizeMake(WindowWidth-width, 900)];
            
        }
        CGFloat heiget=  MAX((labelsize.height+10) + 65-14, 65);
        
        
        _topLine.frame=CGRectMake(40, 0, 0.5, 10);
        _belowLine.frame=CGRectMake(40, 35, 0.5, heiget-35);
        
        _topLine.image=[self createDotLine:CGSizeMake(0.5, 10)];
        _belowLine.image=[self createDotLine:CGSizeMake(0.5, heiget-35)];
        
    }else  if(num==1)//分配
    {
        CGSize labelsize = CGSizeZero;
        CGFloat width=10+35+14;
        if ([order.P_content isKindOfClass:[NSNull class]]||[order.P_content isEqualToString:@""]) {
            
        }else
        {
            
            labelsize = [order.P_content sizeWithFont:F(12) constrainedToSize:CGSizeMake(WindowWidth-width, 900)];
            
        }
        CGFloat heiget=  MAX((labelsize.height+10) + 65-14, 65);
        self.headImg.image=[UIImage imageNamed:@"Ico_progress_gray"];
        
        _topLine.frame=CGRectMake(40, 0, 0.5, 15);
        _belowLine.frame=CGRectMake(40, 25, 0.5, heiget-25);
        _topLine.image=[self createDotLine:CGSizeMake(0.5, 15)];
        _belowLine.image=[self createDotLine:CGSizeMake(0.5, heiget-25)];
        
        
        
        
    }else if(num==0){
            if (self.isRow==YES) {
                self.headImg.image=[UIImage imageNamed:@"Ico_progress_gray"];
                _lineImg.image=[UIImage imageNamed:@""];
                _topLine.frame=CGRectMake(40, 0, 0.5, 15);
                _belowLine.frame=CGRectMake(40, 25, 0, 0);
                _topLine.image=[UIImage imageNamed:@""];
                _belowLine.image=[UIImage imageNamed:@""];
                
            }else
            {
                self.headImg.image=[UIImage imageNamed:@"Ico_progress_gray"];
                _topLine.frame=CGRectMake(40, 0, 0.5, 15);
                _belowLine.frame=CGRectMake(40, 25, 0, 0);
                _topLine.image=[self createDotLine:CGSizeMake(0.5, 15)];
                _belowLine.image=[self createDotLine:CGSizeMake(0.5, 0)];
                
                
            }
    }else

        {
            self.headImg.image=[UIImage imageNamed:@"Ico_progress_gray"];
            _topLine.frame=CGRectMake(40, 0, 0.5, 15);
            _belowLine.frame=CGRectMake(40, 25, 0.5, 65-25);
            _topLine.image=[self createDotLine:CGSizeMake(0.5, 15)];
            _belowLine.image=[self createDotLine:CGSizeMake(0.5, 65-25)];
        }
    
    NSString  *a = order.P_created;
    
    if (a ==nil||[a isEqualToString:@""]) {
        self.timeLab.text =@"";
        
    }else
    {
        NSString *timeString = [order.P_created  substringWithRange:NSMakeRange(0, 16)];
        
        self.timeLab.text=timeString;
    }
    
}
- (UIImage*) createDotLine:(CGSize) size {
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
        UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
    } else {
        UIGraphicsBeginImageContext(size);
    }
    // 绘制改变大小的图片
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    //设置虚线颜色
    CGContextSetStrokeColorWithColor(currentContext, [UIColor lightGrayColor].CGColor);
    //设置虚线宽度
    CGContextSetLineWidth(currentContext, size.width);
    //设置虚线绘制起点
    CGContextMoveToPoint(currentContext, 0, 0);
    //设置虚线绘制终点
    CGContextAddLineToPoint(currentContext, 0, size.height);
    //设置虚线排列的宽度间隔:下面的arr中的数字表示先绘制3个点再绘制1个点
    CGFloat arr[] = {1,1};
    //下面最后一个参数“2”代表排列的个数。
    CGContextSetLineDash(currentContext, 0, arr, 2);
    CGContextDrawPath(currentContext, kCGPathStroke);
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage =UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    //返回新的改变大小后的图片
    return scaledImage;
}



@end
